package enums;

import java.io.Serializable;

/**
 * @description: 数据库字段枚举类基类
 */
public interface BaseEnum extends Serializable {

    int getValue();

    static <T extends Enum<T>> T valueOfEnum(int value, Class<T> enumClass) {
        if (enumClass == null || !BaseEnum.class.isAssignableFrom(enumClass)) {
            throw new IllegalArgumentException("Enum class must implements class BaseEnum.");
        }
        T[] enumItems = enumClass.getEnumConstants();
        for (T item : enumItems) {
            if (((BaseEnum) item).getValue() == value) {
                return item;
            }
        }
        throw new IllegalArgumentException(
                "Cannot parse short value: " + value + " to enum type: " + enumClass.getName());
    }


    /**
     *
     */
    enum FruitEnum implements BaseEnum {
        APPLE(0),
        ORANGE(1),
        BANANA(2);

        private int value;

        FruitEnum(int value) {
            this.value = value;
        }

        @Override
        public int getValue() {
            return this.value;
        }
    }

    /**
     *
     */
    enum AnimalEnum {
        CAT(0)
        ;

        private int value;

        AnimalEnum(int value) {
            this.value = value;
        }

        public int getValue() {
            return this.value;
        }
    }

    public static void main(String[] args) {
        FruitEnum fruit = BaseEnum.valueOfEnum(0, FruitEnum.class);
        System.out.println(fruit);
        AnimalEnum animal = BaseEnum.valueOfEnum(0, AnimalEnum.class);
        System.out.println(animal);
    }
}



